package com.yxzr.youyou.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hjq.baselibrary.base.BaseDialog;
import com.yxzr.youyou.EarnPointsActivity;
import com.yxzr.youyou.IntegralMallActivity;
import com.yxzr.youyou.LeaveMessageActivity;
import com.yxzr.youyou.LoginActivity;
import com.yxzr.youyou.MainActivity;
import com.yxzr.youyou.MyCollectionActivity;
import com.yxzr.youyou.MyMessageActivity;
import com.yxzr.youyou.PeopleDataActivity;
import com.yxzr.youyou.PlayRecordActivity;
import com.yxzr.youyou.R;
import com.yxzr.youyou.RechargeActivity;
import com.yxzr.youyou.RechargeCardActivity;
import com.yxzr.youyou.RechargeRecordActivity;
import com.yxzr.youyou.UpdatePwdActivity;
import com.yxzr.youyou.base.BaseFragment;
import com.yxzr.youyou.bean.UserInfoBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.DataUrl;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.AppManager;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.SharedPreferencesHelper;
import com.yxzr.youyou.utils.StatusUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class MyFragment extends BaseFragment implements View.OnClickListener{
    private ImageView my_img_avatar;
    private TextView my_tv_name,my_tv_type,my_tv_money,my_tv_follow,my_tv_fans,my_tv_score,my_tv_vip;
    private RelativeLayout my_rel_leavemessage,my_rel_message,my_rel_record,my_rel_collect,my_rel_shop,my_rel_score,my_rel_recharge,
            my_rel_rechargecard,my_rel_rechargerecord,my_rel_peopledata,my_rel_updatepwd,my_rel_loginout;
    private LinearLayout my_linear_vip;
    private StatusUtils statusUtils;
    private int isVip = 0;
    private SharedPreferencesHelper sp;
    private UserInfoBean.DataBean userInfoBean;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_my,null);
        sp = new SharedPreferencesHelper(getContext(),"logindata");
        statusUtils = new StatusUtils(getContext());
        initView(v);
        return v;
    }

    private void initView(View v) {
        my_img_avatar = v.findViewById(R.id.my_img_avatar);
        my_tv_name = v.findViewById(R.id.my_tv_name);
        my_tv_type = v.findViewById(R.id.my_tv_type);
        my_tv_money = v.findViewById(R.id.my_tv_money);
        my_tv_follow = v.findViewById(R.id.my_tv_follow);
        my_tv_fans = v.findViewById(R.id.my_tv_fans);
        my_tv_score = v.findViewById(R.id.my_tv_score);
        my_tv_vip = v.findViewById(R.id.my_tv_vip);
        my_linear_vip = v.findViewById(R.id.my_linear_vip);
        my_rel_leavemessage = v.findViewById(R.id.my_rel_leavemessage);
        my_rel_message = v.findViewById(R.id.my_rel_message);
        my_rel_record = v.findViewById(R.id.my_rel_record);
        my_rel_collect = v.findViewById(R.id.my_rel_collect);
        my_rel_shop = v.findViewById(R.id.my_rel_shop);
        my_rel_score = v.findViewById(R.id.my_rel_score);
        my_rel_recharge = v.findViewById(R.id.my_rel_recharge);
        my_rel_rechargecard = v.findViewById(R.id.my_rel_rechargecard);
        my_rel_rechargerecord = v.findViewById(R.id.my_rel_rechargerecord);
        my_rel_peopledata = v.findViewById(R.id.my_rel_peopledata);
        my_rel_updatepwd = v.findViewById(R.id.my_rel_updatepwd);
        my_rel_loginout = v.findViewById(R.id.my_rel_loginout);

        my_rel_leavemessage.setOnClickListener(this);
        my_rel_message.setOnClickListener(this);
        my_rel_record.setOnClickListener(this);
        my_rel_collect.setOnClickListener(this);
        my_rel_shop.setOnClickListener(this);
        my_rel_score.setOnClickListener(this);
        my_rel_recharge.setOnClickListener(this);
        my_rel_rechargecard.setOnClickListener(this);
        my_rel_rechargerecord.setOnClickListener(this);
        my_rel_peopledata.setOnClickListener(this);
        my_rel_updatepwd.setOnClickListener(this);
        my_rel_loginout.setOnClickListener(this);
        my_linear_vip.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.my_rel_leavemessage://求片留言
                intent = new Intent(getContext(), LeaveMessageActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_message://用户消息
                intent = new Intent(getContext(), MyMessageActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_record://播放记录
                intent = new Intent(getContext(), PlayRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_collect://我的收藏
                intent = new Intent(getContext(), MyCollectionActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_shop://积分商城
                intent = new Intent(getContext(), IntegralMallActivity.class);
                intent.putExtra("userinfo",userInfoBean);
                startActivity(intent);
                break;
            case R.id.my_rel_score://赚积分
                intent = new Intent(getContext(), EarnPointsActivity.class);
                intent.putExtra("userinfo",userInfoBean);
                startActivity(intent);
                break;
            case R.id.my_rel_recharge://充值
                intent = new Intent(getContext(), RechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_rechargecard://充值卡
                intent = new Intent(getContext(), RechargeCardActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_rechargerecord://充值记录
                intent = new Intent(getContext(), RechargeRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_peopledata://个人信息
                intent = new Intent(getContext(), PeopleDataActivity.class);
                intent.putExtra("userinfo",userInfoBean);
                startActivity(intent);
                break;
            case R.id.my_rel_updatepwd://修改密码
                intent = new Intent(getContext(), UpdatePwdActivity.class);
                startActivity(intent);
                break;
            case R.id.my_rel_loginout://退出登录
                new BaseDialog.Builder(getActivity())
                        .setContentView(R.layout.setting_dialog_layout)
                        .setWidth(800)
                        .setHeight(500)
                        .setText(R.id.text,"确定要退出登录吗？")
                        .setOnClickListener(R.id.close, new BaseDialog.OnClickListener() {
                            @Override
                            public void onClick(BaseDialog dialog, View view) {
                                dialog.dismiss();
                            }
                        })
                        .setOnClickListener(R.id.shenqing, new BaseDialog.OnClickListener() {
                            @Override
                            public void onClick(BaseDialog dialog, View view) {
                                dialog.dismiss();
                                loginout();
                            }
                        })
                        .show();
                break;
            case R.id.my_linear_vip://充值
                if (isVip == 0){
                    intent = new Intent(getContext(), RechargeActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    private void loginout() {
        sp.put("phone","");
        sp.put("token","");
        sp.put("userid","");
        sp.put("avatar","");
        sp.put("username","");

        AppManager.getInstance().finishAllActivity();
        Intent intent = new Intent(getContext(), MainActivity.class);
        startActivity(intent);
    }

    private void initData(){
        RequestUtil.getInstence().API().getUserInfo((String) sp.getSharedPreference("token",""))
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        UserInfoBean bean = gson.fromJson(t,UserInfoBean.class);

                        userInfoBean = bean.getData();

                        Glide.with(getContext())
                                .setDefaultRequestOptions(
                                        new RequestOptions()
                                                .frame(1000000)
                                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                                .centerCrop()
                                                .error(R.mipmap.default_avatar)
                                                .placeholder(R.mipmap.default_avatar))
                                .load(DataUrl.imgUrl+userInfoBean.getHead_pic())
                                .apply(RequestOptions.bitmapTransform(new CropCircleTransformation()))
                                .into(my_img_avatar);

                        my_tv_name.setText(bean.getData().getUsername());
                        my_tv_follow.setText(bean.getData().getFollow());
                        my_tv_fans.setText(bean.getData().getFans());
                        my_tv_score.setText(bean.getData().getIntegral());

                        if ("0".equals(bean.getData().getVip())){
                            my_tv_type.setVisibility(View.VISIBLE);
                            my_tv_vip.setText("开通VIP");
                        }else {
                            my_tv_type.setVisibility(View.GONE);
                            my_tv_vip.setText("VIP(剩余时间："+bean.getData().getVip_time()+")");
                        }

                        if ("0".equals(bean.getData().getVip())){
                            isVip = 0;
                        }else {
                            isVip = 1;
                        }
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (statusUtils.isLogin()){
            initData();
        }
    }
}
